$(document).ready(function() {


    $("#reset").click(function() {
        url = "/demand/data_entry";
        document.location.href = url;
    });



    $("#home_address").click(function() {
        show_address('home_address');
    });


    $("#company_address").click(function() {
        show_address('company_address');
    });

    $("#building_name")
            .bind('click', function() {
                //alert('aaa');
                $(this).autocomplete("search");
            })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {

                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                return false;
                                                    }));

                            return false;
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });
//alert("edit");

    $("#name").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/client_contact",
                                    dataType: "json",
                                    data: {
                                        query: request.term
                                    },
                                    success: function(data) {


                                            response($.map(data, function(item) {
                                                return {
                            label: item.name + "/" + item.recommended,
                            value: item.name,
                            mobile: item.mobile,
                            client_id: item.client_id,
                            home_phone: item.home_phone,
                            ref_client_verify_code: item.ref_client_verify_code,
                            company_phone: item.company_phone,
                            email: item.email,
                            home_address: item.home_address,
                            home_building_name: item.home_building_name,
                            company_address: item.company_address,
                            company_name: item.company_name,
                            //contact_name:item.contact_name,
                            //contact_home_phone:item.contact_home_phone
                                                }
                                            }));
                                    }
                            });
                    },
        select: function(e, ui) {

            $("#mobile").val(ui.item.mobile).attr("readonly", "true").css("background", "aliceblue");
            $("#home_phone").val(ui.item.home_phone).attr("readonly", "true").css("background", "aliceblue");
            $("#company_phone").val(ui.item.company_phone).attr("readonly", "true").css("background", "aliceblue");
            
            $("#home_address").val(ui.item.home_address).attr("readonly", "true").css("background", "aliceblue")
            $("#email").val(ui.item.email).attr("readonly", "true").css("background", "aliceblue");
            $("#home_building_name").val(ui.item.home_building_name).attr("readonly", "true").css("background", "aliceblue");
            $("#company_address").val(ui.item.company_address).attr("readonly", "true").css("background", "aliceblue");
            $("#company_name").val(ui.item.company_name).attr("readonly", "true").css("background", "aliceblue");


            $("#client_id").val(ui.item.client_id);
            $("#ref_client_verify_code").val(ui.item.ref_client_verify_code);

            
            $("#client_contact_id").val('');
            $("#building_name").unbind();
            $("#company_address").unbind();
            $("#home_address").unbind();
            $("#contact_name").val('');
            $("#contact_phone").val('');


        },
                    minLength: 1
     	});

    //聯絡人自動下拉
    $("#contact_name").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/get_contact_json",
                                    dataType: "json",
                                    data: {
                    query: $("#ref_client_verify_code").val()
                                    },
                                    success: function(data) {

                                            response($.map(data, function(item) {
                                                return {
                            label: item.name ,
                            value: item.name,
                            mobile: item.mobile,
                            client_contact_id: item.client_contact_id,
                                                }
                                            }));
                                    }
                            });
                    },
        select: function(e, ui) {

            $("#contact_phone").val(ui.item.mobile).attr("readonly", "true").css("background", "aliceblue");
            $("#client_contact_id").val(ui.item.client_contact_id);

        },
                    minLength: 1
     	});



    $("#head").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/users_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.name,
                            value: item.name
                                                }
                                            }));
                                    }
                            });
                    },
                    minLength: 1
     	});

    $("#original_broker").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/users_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.name,
                            value: item.name
                                                }
                                            }));
                                    }
                            });
                    },
                    minLength: 1
     	});

    $("#broker").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/users_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.name,
                            value: item.name
                                                }
                                            }));
                                    }
                            });
                    },
                    minLength: 1
     	});

    $("input").keypress(function(e) {
        var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        if (keyCode == 13) {
            var i;
            for (i = 0; i < this.form.elements.length; i++) {
                if (this == this.form.elements[i])
                    break;
            }
            i = (i + 1) % this.form.elements.length;
            this.form.elements[i].focus();
            return false;
        } else {
            return true;
        }
    });

    $("select").keypress(function(e) {
        var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        if (keyCode == 13) {
            var i;
            for (i = 0; i < this.form.elements.length; i++) {
                if (this == this.form.elements[i])
                    break;
            }
            i = (i + 1) % this.form.elements.length;
            this.form.elements[i].focus();
            return false;
        } else {
            return true;
        }
    });

});